package com.cloudlibrary.mapper;

import com.cloudlibrary.domain.Record;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
public interface RecordMapper {
    @Insert("insert into record(record_id,record_bookname,record_bookisbn, " +
            "record_borrower,record_borrowtime,record_remandtime) " +
            "values(NULL,#{bookname},#{bookisbn},#{borrower}," +
            "#{borrowTime},#{remandTime})")
    @Results(id = "RecordMap", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示持久化类属性名称。
            @Result(id = true, column = "record_id", property = "id"),
            @Result(column = "record_bookname", property = "bookname"),
            @Result(column = "record_bookisbn", property = "bookisbn"),
            @Result(column = "record_borrower", property = "borrower"),
            @Result(column = "record_borrowtime", property = "borrowTime"),
            @Result(column = "record_remandtime", property = "remandTime"),
    })
    //新增借阅记录
    Integer addRecord(Record record);


    @Select({"<script>" +
            "SELECT * FROM record " +"where record_id is not null " +
            "<if test='bookname != null'> "+
            "AND  record_bookname  like  CONCAT('%',#{bookname},'%')</if>" +
            "<if test='borrower != null'>" +
            "AND record_borrower like  CONCAT('%', #{borrower},'%') </if>" +
            "order by record_id" +
            "</script>"})
    @ResultType(Record.class)
    @Results(id = "RecordMap1", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示持久化类属性名称。
            @Result(id = true, column = "record_id", property = "id"),
            @Result(column = "record_bookname", property = "bookname"),
            @Result(column = "record_bookisbn", property = "bookisbn"),
            @Result(column = "record_borrower", property = "borrower"),
            @Result(column = "record_borrowtime", property = "borrowTime"),
            @Result(column = "record_remandtime", property = "remandTime"),
    })
    /*查询借阅记录*/
    Page<Record> searchRecords(Record record);
}
